<?php

declare(strict_types=1);

namespace App\Factory\Declaration;

use App\Factory\Wechat\Factory as WechatFactory;
use App\Utils\Other\Payment\Sandpay\Declares;
use EasyWeChat\Kernel\HttpClient\HttpClientMethods;
use Hyperf\Context\Context;
use App\Utils\Other\Safepay\Declare\Factory as SafePayFactory;
use App\Utils\Other\Payment\Neopay\Declares as NeopayDeclares;
use App\Utils\Other\Payment\Sumpay\Declares as SumpayDeclares;
use App\Utils\Other\Payment\Chinapnrpay\Declares as ChinapnrpayDeclares;
use function Hyperf\Support\make;

Class Factory {

    /**
     * Safepay支付单推送
     * @param array $params
     * @param string $appId
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function safepayMini(array $params, string $appId=''): array
    {
        /** @var SafePayFactory $declaration */
        $declaration = make(SafePayFactory::class);

        return $declaration->handle($params);
    }

    /**
     * 易生报关
     * @param array $params
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function neopayMini(array $params):array
    {
        /** @var NeopayDeclares $declaration */
        $declaration = make(NeopayDeclares::class);
        $declaration->setConfig(Context::get('pay_config_array'));

        return $declaration->push($params);
    }

    /**
     * 衫德支付单推送
     * @param array $params
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function sandpayMini(array $params): array
    {
        /** @var Declares $declares */
        $declares = make(Declares::class);
        $declares->setConfig(Context::get('pay_config_array'));

        return $declares->push($params);
    }

    /**
     * 微信报关
     * @param array $params
     * @return array
     * @throws \EasyWeChat\Kernel\Exceptions\BadResponseException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \EasyWeChat\Pay\Exceptions\InvalidSignatureException
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
     * @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface
     * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
     * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
     */
    public function wechatMini(array $params): array
    {
        $config = Context::get('pay_config_array');
        /** @var WechatFactory $pay */
        $pay = make(WechatFactory::class);
        $app = $pay->pay($config);
        $account = $app->getMerchant();
        $params['appid'] = $config['app_id'];
        $params['mch_id'] = (string)$account->getMerchantId();
        $params['customs'] = $config['customs'];
        $params['mch_customs_no'] = $config['pre_ebp_code'];
        $params['nonce_str'] = null;

        /** @var HttpClientMethods $client */
        $client = $app->getClient();

        $response = $client->postXml('cgi-bin/mch/customs/customdeclareorder', $params);
        $data = $response->toArray();

        return [
            $params, $data, $data,
            'https://api.mch.weixin.qq.com/cgi-bin/mch/customs/customdeclareorder'
        ];
    }

    /**
     * 统统付支付单推送
     * @param array $params
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function sumpayMini(array $params): array
    {
        /** @var SumpayDeclares $declares */
        $declares = make(SumpayDeclares::class);
        $declares->setConfig(Context::get('pay_config_array'));

        return $declares->push($params);
    }

    /**
     * 统统付支付单查询
     * @param array $params
     * @return array
     */
    public function sumpayMiniQuery(array $params): array
    {
        /** @var SumpayDeclares $declares */
        $declares = make(SumpayDeclares::class);
        $declares->setConfig(Context::get('pay_config_array'));

        return $declares->query($params);
    }

    /**
     * 汇付海关推送
     * @param array $params
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function chinapnrpayMini(array $params): array
    {
        /** @var ChinapnrpayDeclares $declares */
        $declares = make(ChinapnrpayDeclares::class);
        $declares->setConfig(Context::get('pay_config_array'));

        return $declares->push($params);
    }
}